package 数组.搜索二维矩阵;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/7 16:29
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int[][]  array = {{1}};
        System.out.println(searchMatrix(array, 1));
    }

    //二分法
    public static boolean searchMatrix(int[][] matrix, int target) {
        int left = 0;
        int right = matrix[0].length - 1;
        int low = 0;
        int high = matrix.length - 1;
        //先对列二分
        while (low < high){
            int mid = (low + high) / 2;
            if (matrix[mid][right] == target){
                return true;
            }else if (matrix[mid][right] < target){
                low = mid + 1;
            }else if (matrix[mid][right] > target){
                high = mid;
            }
        }
        //再次对行二分
        while (left <= right){
            int mid = (left + right) / 2;
            if (matrix[low][mid] == target){
                return true;
            }else if (matrix[low][mid] < target){
                left = mid + 1;
            }else if (matrix[low][right] > target){
                right = mid - 1;
            }
        }
        return false;
    }
}
